package com.YCH.user.service.Impl;

import com.YCH.user.service.AppFollowBehaviorService;
import com.YCH.model.behavior.dtos.FollowBehaviorDto;
import com.YCH.model.behavior.pojos.TbFollowBehavior;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.TbFollowBehaviorMapper;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@SuppressWarnings("all")
public class AppFollowBehaviorServiceImpl implements AppFollowBehaviorService {
    Logger logger  = LoggerFactory.getLogger(AppFollowBehaviorServiceImpl.class);
    @Autowired
    private TbFollowBehaviorMapper tbFollowBehaviorMapper;

    @Override
    @Async
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto){
        logger.info("异步存储关注行为：{}",dto);
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        // 保存行为
        TbFollowBehavior alb = new TbFollowBehavior();
        alb.setUserId(userId);
        alb.setCreatTime(new Date());
        alb.setArticleId(dto.getArticleId());
        alb.setFollowId(dto.getFollowId());
        return ResponseResult.okResult(tbFollowBehaviorMapper.insert(alb));
    }
}
